﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BestHTTP;
using Object = System.Object;

public class BestHttpUtil: MonoBehaviour
{
    public static BestHttpUtil Instance;

    private void Awake()
    {
        Instance = this;
    }

    /// <summary>
    /// HTTP get请求
    /// </summary>
    /// <param name="uri"></param>
    /// <param name="callBack"></param>
    public void SendHTTPRequestWithCallBack(string uri, Action<String> callBack)
    {
        new HTTPRequest(new Uri(uri), (request, response) =>
        {
            //动作
            if (callBack != null)
            {
                callBack(response.DataAsText);
            }
            else
            {
                Debug.LogError("GGL_  BestHttpUtil 没有回调方法!!!");
            }

        }).Send();
        
    }


    public void SendHTTPPostRequestWithCallBack(string uri, Dictionary<string, string> postFields, Action<String> callBack)
    {
        HTTPRequest rep =new HTTPRequest(
            new Uri(uri),
            HTTPMethods.Post,
            (request, response) =>
            {
                //动作
                callBack(response.DataAsText);

            }
        );
        foreach (var field in postFields)
        {
            rep.AddField(field.Key, field.Value);
        }
        rep.Send();

    }


}
